package top.aigoo.project01.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.aigoo.project01.entity.PersonInfo;
import top.aigoo.project01.entity.dto.PersonInfoExecution;
import top.aigoo.project01.enums.PersonInfoStateEnum;
import top.aigoo.project01.mapper.PersonInfoMapper;
import top.aigoo.project01.service.PersonInfoService;
import top.aigoo.project01.utils.PageCalculator;

/**
 * @Description: 请填写该类完成的功能
 * @Author: Mr.OYSHUO.欧阳大叔  mail:1642302522@qq.com
 * @Version 1.0.0
 * @Date: 2021/9/10 23:37
 */
@Service
public class PersonInfoServiceImpl implements PersonInfoService {

    private static Logger logger = LoggerFactory.getLogger(PersonInfoServiceImpl.class);

    @Autowired
    private PersonInfoMapper personInfoMapper;


    @Override
    public PersonInfo getPersonInfoById(Long userId) {
        return personInfoMapper.queryPersonInfoById(userId);
    }

    @Override
    public PersonInfoExecution getPersonInfoList(PersonInfo personInfoCondition, int pageIndex, int pageSize) {
        int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);

        return null;
    }

    @Override
    public PersonInfoExecution addPersonInfo(PersonInfo personInfo) {
        if (personInfo == null) {
            return new PersonInfoExecution(PersonInfoStateEnum.EMPTY);
        } else {
            try {
                int effectedNum = personInfoMapper.insertPersonInfo(personInfo);
                if (effectedNum <= 0) {
                    return new PersonInfoExecution(
                            PersonInfoStateEnum.INNER_ERROR);
                } else {// 创建成功
                    personInfo = personInfoMapper.queryPersonInfoById(personInfo.getUserId());
                    return new PersonInfoExecution(PersonInfoStateEnum.SUCCESS, personInfo);
                }
            } catch (Exception e) {
                throw new RuntimeException("addPersonInfo error: " + e.getMessage());
            }
        }
    }

    @Override
    public PersonInfoExecution modifyPersonInfo(PersonInfo personInfo) {
        return null;
    }
}
